package com.huitone.smspfm.socket.protocol.sgip.message.msgbody;

import java.io.UnsupportedEncodingException;

import com.bin.common.utils.ArrayUtils;
import com.bin.common.utils.StringUtils;
import com.huitone.smspfm.socket.protocol.sgip.constant.Constant;
import com.huitone.smspfm.socket.protocol.sgip.conversion.TransferConverter;

/**   
* @Title: SubmitResp.java 
* @Package com.huitone.smspfm.socket.protocol.sgip.msgbody 
* @Description: TODO(用一句话描述该文件做什么) 
* @author ybliang   
* @date 2017年3月23日 上午11:05:30 
* @version V1.0   
*/
public class Submit2Resp extends Resp {
	
	/**
	 * @字节长度 21
	 * @数据类型 Text
	 * @DESC
	 * 	业务端编码
	 */
	private byte[] srvNode = new byte[21];
	
	/**
	 * @字节长度 10
	 * @数据类型 Text
	 * @DESC
	 * 	短信业务编码
	 */
	private byte[] serviceCode = new byte[10];
	
	/**
	 * @字节长度 21
	 * @数据类型 Text
	 * @DESC
	 * 	发送端口号
	 */
	private byte[] sendPort = new byte[21];
	
	/**
	 * @字节长度 21
	 * @数据类型 Text
	 * @DESC
	    接收短消息的手机号，手机号码前加“86”国别标志
	 */
	private byte[] userNumber = new byte[13];
	
	private byte[] orgSrvNode = new byte[21];
	
	private byte[] orgSerialNoLength = new byte[4];
	
	private byte[] orgSerialNo;
	
	private byte[] submit2Resp = null;
	
	public static Submit2Resp build(byte[] bytes) {
		
		Submit2Resp submit2Resp = new Submit2Resp();
		
		int index = 0;
		
		submit2Resp.result = ArrayUtils.subarray(bytes, index, index += 1);
		submit2Resp.srvNode = ArrayUtils.subarray(bytes, index, index += 21);
		submit2Resp.serviceCode = ArrayUtils.subarray(bytes, index, index += 10);
		submit2Resp.sendPort = ArrayUtils.subarray(bytes, index, index += 21);
		submit2Resp.userNumber = ArrayUtils.subarray(bytes, index, index += 13);
		submit2Resp.orgSrvNode = ArrayUtils.subarray(bytes, index, index += 21);
		submit2Resp.orgSerialNoLength = ArrayUtils.subarray(bytes, index, index += 4);
		submit2Resp.orgSerialNo = ArrayUtils.subarray(bytes, index, index += submit2Resp.getOrgSerialNoLength());
		
		return submit2Resp;
	}
	
	public static Submit2Resp build(int result, String userNumber, String srvNode, String serviceCode, String sendPort, String orgSrvNode, String orgSerialNo) {
		
		Submit2Resp submit2Resp = new Submit2Resp();
		
		submit2Resp.setResult(result);
		submit2Resp.setSrvNode(srvNode);
		submit2Resp.setServiceCode(serviceCode);
		submit2Resp.setSendPort(sendPort);
		submit2Resp.setUserNumber(userNumber);
		submit2Resp.setOrgSrvNode(orgSrvNode);
		submit2Resp.setOrgSerialNo(orgSerialNo);
		
		return submit2Resp;
	}

	@Override
	public byte[] toByteArray() {
		
		if (submit2Resp == null) {
			submit2Resp = ArrayUtils.addAll(result, srvNode, serviceCode, sendPort, userNumber, orgSrvNode, orgSerialNoLength, orgSerialNo);
		}
		
		return submit2Resp;
	}

	@Override
	public int length() {
		return toByteArray().length;
	}
	
	public String getSrvNode() {
		return TransferConverter.lj2Str(srvNode);
	}

	public void setSrvNode(String srvNode) {
		this.srvNode = TransferConverter.toLJ(this.srvNode, srvNode);
	}

	public String getServiceCode() {
		return TransferConverter.lj2Str(serviceCode);
	}

	public void setServiceCode(String serviceCode) {
		this.serviceCode = TransferConverter.toLJ(this.serviceCode, serviceCode);
	}

	public String getSendPort() {
		return TransferConverter.lj2Str(sendPort);
	}

	public void setSendPort(String sendPort) {
		this.sendPort = TransferConverter.toLJ(this.sendPort, sendPort);
	}
	
	public String getUserNumber() {
		return TransferConverter.lj2Str(userNumber);
	}

	public void setUserNumber(String userNumber) {
		this.userNumber = TransferConverter.toLJ(this.userNumber, StringUtils.phNumRem86(userNumber));
	}

	public String getOrgSrvNode() {
		return TransferConverter.lj2Str(orgSrvNode);
	}

	public void setOrgSrvNode(String orgSrvNode) {
		this.orgSrvNode = TransferConverter.toLJ(this.orgSrvNode, orgSrvNode);
	}

	public int getOrgSerialNoLength() {
		return TransferConverter.bE2Int4(orgSerialNoLength);
	}

	public void setOrgSerialNoLength(int orgSerialNoLength) {
		this.orgSerialNoLength = TransferConverter.toBE4(orgSerialNoLength);
	}

	public String getOrgSerialNo() {
		
		String result = null;
		
		try {
			result = new String(this.orgSerialNo, Constant.defaultEncoding);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return result;
	}

	public void setOrgSerialNo(String orgSerialNo) {
		
		if (!StringUtils.isEmpty(orgSerialNo)) {
			
			try {
				byte[] bytes = orgSerialNo.getBytes(Constant.defaultEncoding);
				setOrgSerialNoLength(bytes.length);
				this.orgSerialNo = bytes;
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		} else {
			this.orgSerialNo = new byte[]{};
			setOrgSerialNoLength(0);
		}
	}

	@Override
	public String toString() {
		return "Submit2Resp [业务端号=" + getSrvNode() + ", 业务类型=" + getServiceCode()
				+ ", 扩展码=" + getSendPort() + ", 用户号码=" + getUserNumber() + ", 旧客户端号="
				+ getOrgSrvNode() + ", 旧序列号=" + getOrgSerialNo() + ", 处理结果=" + getResult() + "]";
	}

	@Override
	public String toSimpleString() {
		return getResult() + "";
	}
}
